use alice2d::{ui::egui::{Sense, Label, Id, Ui, self}, scene::{TagComponent, TransformComponent, SpriteRendererComponent, CameraComponent, Entity, SceneServer, Scene}, ecs::entity, math::Vector3f, color::Rgba};

use crate::editor::EditorLayer;



pub struct ScenePanel;


impl ScenePanel {
    pub fn draw_scene_tree(ctx: &mut egui::Context,layer:&mut EditorLayer) {
        let server = SceneServer::get_server_mut();
        let scene = server.get_active_scene_mut();
        alice2d::ui::egui::SidePanel::left("scene_panel")
        .resizable(true)
        .default_width(200.0)
        .width_range(80.0..=400.0)
        .show(ctx, |ui| {
            ui.vertical_centered(|ui| {
                ui.heading("Scene Panel");
            });
            ui.vertical(|ui| {
                alice2d::ui::egui::CollapsingHeader::new("Scene")
                    .default_open(true)
                    .show(ui, |ui| {
                     
                      Self::draw_scene_node(layer,scene.root , scene, ui);

               
                     
                    });
            });
        })
        .response
        .context_menu(|ui| {

            // create game_object instance

            Self::create_game_object_instance(ui,layer,scene);
      
        });
        
          
    }

    fn draw_scene_node(layer:&mut EditorLayer, entity: entity::Entity, scene:&Scene, ui: &mut Ui) {
      
        if let Some(entity_node) = scene.get_entity(&entity) {
            let tag = entity_node
                .get_component::<TagComponent>(&scene)
                .unwrap();

            let mut selected = false;
            let id = entity_node.get_uuid(&scene);
            if let Some(select) = &layer.select_entity {
                selected = entity_node.handle.eq(select);
            }

            let (response, _, _) = alice2d::ui::egui::collapsing_header::CollapsingState::load_with_default_open(
                ui.ctx(),
                Id::new(id),
                selected,
            )
            .show_header(ui, |ui| {
           
               if ui.add(Label::new(&tag.tag).sense(Sense::click())).clicked() {
                layer.select_entity = Some(entity);
               }
            })
            .body(|ui| {
               
                for entity in entity_node.get_child_nodes() {
                   Self::draw_scene_node(layer,*entity,scene, ui) 
                }
            });

            if response.clicked() {
                layer.select_entity = Some(entity);
            }
        }

  
    }


    fn create_game_object_instance(ui: &mut egui::Ui,layer:&mut EditorLayer,scene:&mut Scene){
        if ui.button("Empty Gameobject").clicked() {
            let mut entity = scene.create_entity("gameobject");
            entity.add_component(
                scene,
                TransformComponent::new(Vector3f::new(0.0, 0.0, 0.0)),
            );
            Self::create_entity(layer,entity,scene);

            ui.close_menu();
        }
        ui.menu_button("2D", |ui| {
   
            if ui.button("Quad").clicked() {
                let mut entity = scene.create_entity("Quad");
                entity.add_component(
                    scene,
                    TransformComponent::new(Vector3f::new(0.0, 0.0, 0.0)),
                );
                entity.add_component(
                    scene,
                    SpriteRendererComponent::new(Rgba::WHITE),
                );
                
                Self::create_entity(layer,entity,scene);
    
                ui.close_menu();
            }
        });

        if ui.button("Camera").clicked() {
            let mut entity = scene.create_entity("Camera");
            entity.add_component(
                scene,
                TransformComponent::new(Vector3f::new(0.0, 0.0, 0.0)),
            );
            entity.add_component(
                scene,
                CameraComponent::orthographic(),
            );
            
            Self::create_entity(layer,entity,scene);

            ui.close_menu();
        }
      


    }

    pub fn create_entity(layer:&mut EditorLayer,mut entity:Entity,scene:&mut Scene) {
        if let Some(select_node) = &mut layer.select_entity {
            if let Some(select_node) = scene.get_entity_mut(select_node) {
                select_node.add_child(&mut entity);
            }
        }else{
            let root = scene.root;
            if let Some(root_node) = scene.get_entity_mut(&root) {
                root_node.add_child(&mut entity);
            }
        }
    }
}

